package com.taritari.journal.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.taritari.journal.entity.vo.ERNIEBotVo;
import com.taritari.journal.entity.vo.SdVo;
import com.taritari.journal.entity.vo.SentimentClassifyV1Vo;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author taritari
 * @date 2024-01-14 21:52
 * @description 百度api开放平台的接口
 */
@Component
public class BaiDuApiUtil {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 从用户的AK，SK生成鉴权签名（Access Token）
     *
     * @return 鉴权签名（Access Token）
     * @throws
     */
    public String getAccessToken(){
        String client_id = "LjdTeLCE7PSnULu9IrKCsbsu";
        String client_secret = "YAGyAqRlThAoGb70fsf93mdxkxREoN7A";
        String url = "https://aip.baidubce.com/oauth/2.0/token?client_id="+client_id+"&client_secret="+client_secret+"&grant_type=client_credentials";
        HttpRequest request = HttpRequest.post(url)
                .contentType("application/json")
                .header("Accept", "application/json");

        String response = request.execute().body();
        JSONObject entries = new JSONObject(response);
        String accessToken = entries.getStr("access_token");
        stringRedisTemplate.opsForValue().set("accessToken",accessToken,30 * 24 * 60 * 60, TimeUnit.SECONDS);
        return accessToken;
    }
    /**
     * 情感趋势分析接口
     * */
    public SentimentClassifyV1Vo baiDuSentimentClassify(String accessToken,String body){
        String url = "https://aip.baidubce.com/rpc/2.0/nlp/v1/sentiment_classify?charset=utf-8&access_token=" + accessToken;
        HttpRequest request = HttpRequest.post(url)
                .contentType("application/json")
                .body(body)
                .header("Content-Type", "application/json")
                .header("Accept", "application/json");

        HttpResponse response = request.execute();
        String responseBody = response.body();
        SentimentClassifyV1Vo sentimentClassifyV1Vo = JSONUtil.toBean(responseBody, SentimentClassifyV1Vo.class);
        return sentimentClassifyV1Vo;
    }
    /**
     * 文心4.0接口
     * @param accessToken 鉴权签名（Access Token）
     * @param content 文本内容
     * @return 返回结果封装至VO
     * */
    public ERNIEBotVo wenXinWorkShop(String accessToken, String content){
        String url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/chat/completions_pro?access_token=" + accessToken;
        HttpRequest request = HttpRequest.post(url)
                .contentType("application/json")
                .body(content)
                .header("Content-Type", "application/json")
                .header("Accept", "application/json");

        HttpResponse response = request.execute();
        String responseBody = response.body();
        ERNIEBotVo ernieBotVo = JSONUtil.toBean(responseBody, ERNIEBotVo.class);
        return ernieBotVo;
    }
    /**
     * 千帆平台token
     * 从用户的AK，SK生成鉴权签名（Access Token）
     *
     * @return 鉴权签名（Access Token）
     * @throws IOException IO异常
     */
    public String getQianFanToken(){
        String client_id = "kPC1wnipTFCzs9gHkqZ4Ga4R";
        String client_secret = "G1zLijecXswqKSEx1h2xjwwXMbN1frLu";
        String url = "https://aip.baidubce.com/oauth/2.0/token?client_id="+client_id+"&client_secret="+client_secret+"&grant_type=client_credentials";
        HttpRequest request = HttpRequest.post(url)
                .contentType("application/json")
                .header("Accept", "application/json");

        String response = request.execute().body();
        JSONObject entries = new JSONObject(response);
        String accessToken = entries.getStr("access_token");
        stringRedisTemplate.opsForValue().set("qianFanaccessToken",accessToken,30 * 24 * 60 * 60, TimeUnit.SECONDS);
        return accessToken;
    }
    /**
     * 文生图接口
     * @param accessToken 鉴权签名（Access Token）
     * @param content 文本内容
     * @return 返回结果封装至VO
     * */
    public SdVo stableDiffusion(String accessToken, String content){
        String url = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/wenxinworkshop/text2image/sd_xl?access_token=" + accessToken;
        HttpRequest request = HttpRequest.post(url)
                .contentType("application/json")
                .body(content)
                .header("Content-Type", "application/json")
                .header("Accept", "application/json");

        HttpResponse response = request.execute();
        String responseBody = response.body();
        SdVo sdVo = JSONUtil.toBean(responseBody, SdVo.class);
        return sdVo;
    }

}
